<template>
	<view class="container">
		<view class="top">
			<image class="image" src="https://m.nocexpo.com/statics/js/modules/h5/img/beijing.png" mode="widthFix" ></image>
			<view class="texts">
			<view class="a">Hi,</view>
				<view class="b">欢迎来到金诺云展~</view>
			</view>
		</view>
		
     <view style="position: relative;bottom: 12%;"  >
		 <view class="form">
		 	<view class="item" style="margin-left: 10px;">
		 		<image class="img" :src="imgUrl + '/statics/js/modules/applet/img/shoujihao.png'" mode="widthFix" />
		 			<text style="margin-left: 5px; font-size: 28rpx;">手机号</text>
		 	</view>
		 	<view style="margin-left: 26px; border-bottom: 1px solid #ccc ; width: 320px; padding-bottom: 20rpx;">
		 		<input placeholder="请输入手机号" style="font-size: 15px;" v-model="nocUser.phone"/>
		 	</view>
		 	<view class="item" style="margin-left: 10px; margin-top: 20rpx;">
		 		<image  class="img"  :src="imgUrl + '/statics/js/modules/applet/img/yanzhengma.png'" mode="widthFix" />
		 		<text style="margin-left: 5px; font-size: 28rpx;">验证码</text>
		 	</view>
		 	<view style="margin-left: 26px; border-bottom: 1px solid #ccc ; width: 320px; display: flex; justify-content:flex-start;padding-bottom: 20rpx;">
		 		<input placeholder="请输入验证码" style="font-size: 15px;"  v-model="nocUser.verificationCode"/>
				<text class="huoqu" @click="getCheckCode">{{sendMsg}}</text>
		 	</view>
		 </view>
    <view class="denglu" @click="login">登录</view>
	<view class="" style="display: flex; justify-items:end">
		<view class="zhanghao">已有账号？</view>
		<navigator class="mima" open-type="redirect"  hover-class="none" url="../login/loginBy">密码登录</navigator>
	</view>
	</view>
 </view>
</template>

<script>
import login from '@/api/login.js';	
import commonApi from '@/utils/commonMethod.js';
import indexApi from '@/utils/request/index.js';
export default {
	data() {
		return {
      countdown: 60,
      nocUser: {
        phone: "",
        password: '',
        verificationCode: '',
        isChecked: false
      },
      sendFlag: true,
      sendMsg: '获取验证码',
	  currentUser:{},
	   imgUrl: indexApi.baseUrl
    };
	},
  mounted(){
    let phone =  '';
    if(phone){
      this.nocUser.phone = phone
    }
  },
	methods: {
    //登陆方法
    otherLogin() {
      //调用登录方法
      this.login(this.nocUser.phone,this.nocUser.verificationCode,);
    },
    getCheckCode(e) {
      if (!this.sendFlag) {
        return;
      }
	  if(this.countdown==60){
		  this.settime();
		  //验证用户是否存在
		  login.getCheckCode(
		      {phone: this.nocUser.phone}
		  ).then(response => {
		    if (response.data.status == 1) {
		      login.slideCheck(this.nocUser.phone);
		    } else if (response.data.status == 2) {
		      layer.open({
		        content: '账号已被封禁，请联系平台客服',
		        btn: ['确定'],
		        yes: function (index) {
		          layer.close(index)
		        }
		      });
		    } else {
		      layer.open({
		        anim: '提示',
		        content: '该账号不存在,请注册',
		        btn: ['立即注册'],
		        yes: function (index) {
		          layer.close(index)
		          this.toRegistered()
		          return;
		        }
		      });
		    }
		  }).catch(function (error) {
		    uni.showToast({
		      "title": "已发送",
		      "icon": "none",
		    });
		  });
	  }
     
    },
    //验证码定时器,60秒后可以再次获取
	settime(){
	 var timeId =setInterval(()=>{
		 this.countdown--;
		   this.sendMsg="重新获取"+this.countdown;
	    if(this.countdown <= 0){
	        clearInterval(timeId)
			this.countdown=60;
			this.sendMsg="获取验证码";
	    }
	 },1000);
	},
    //跳转到注册页面
    toRegistered() {
      if (this.nocUser.phone) {
        window.location.href = baseURL + "/h5/registered.html?phone=" + this.nocUser.phone
      } else {
        window.location.href = baseURL + "/h5/registered.html"
      }
    }, 
    // 获取手机号
    getPhoneNumber(e) {
      let that = this;
      if(!this.isChecked){
        uni.showToast({
          "title": '请先同意协议',
          "icon": "none",
        });
        return;
      }
      login.getPhoneNumber({code: e.detail.code}).then(function (res) {
        if(res.data.code == 0){
          //手机号
          that.nocUser.phone = res.data.phonenumber;
          //调用登录方法
          that.login(that.nocUser.phone, '052930');
        }
      })

    },
	getCurrentUser(){
		commonApi.currentUserAxios().then(res =>{
			this.currentUser = res.data.session_user_h5;
			uni.setStorageSync('userId',res.data.session_user_h5.id)
			commonApi.updLogUserLogin(this.currentUser);
			commonApi.updUserInfo(this.currentUser.id,this.currentUser.type)
		});
	},	

    // 公共登录方法
    async login() {
		let that = this
      //登录
      //	todo 表单验证功能待做
      login.login (
	  '',
	  {
	    phone:this.nocUser.phone,
	    verificationCode:this.nocUser.verificationCode,
	    appletSourceUserId : uni.getStorageSync("sourceId")
	  }
	  
	  ).then(function (response) {
        if (response.data.code === 0) {
          console.log(response.cookies)
          //记录cookies
          if (response.cookies && response.cookies.length > 0) {
            for(let i=0;i<response.cookies.length;i++) {
              let cookie = response.cookies[i];
              if (cookie.indexOf('SESSION')>=0) {
                uni.setStorageSync('cookie', cookie);
              } 	
            }
          }
		  that.getCurrentUser();
		  
		  
		  
          //登录后清除弹窗状态
          //localStorage.removeItem("ifPop");
          if (!response.data.url) {
            uni.switchTab({
              url: '/pages/tabBar/index/index'
            })
          }
          if (response.data.url.includes("/h5login")) {
            uni.switchTab({
              url: '/pages/tabBar/index/index'
            })
          } else if (response.data.url.includes("index") || response.data.url.includes("cloudhome")
              || response.data.url.includes("product") || response.data.url.includes("cloudcomp")
              || response.data.url.includes("cloudlive") || response.data.url.includes("takePartInAct")
              || response.data.url.includes("dailyTask")) {
            //window.location.href = baseURL + response.data.url
            uni.switchTab({
              url: '/pages/tabBar/index/index'
            })
          } else {
            //window.location.href = baseURL + "h5/index.html"
            uni.switchTab({
              url: '/pages/tabBar/index/index'
            })
          }
        } else {
          layer.open({
            anim: '提示',
            content: response.data.msg,
            btn: ['确定'],
            yes: function (index) {
              layer.close(index)
            }
          });
        }
      }).catch(function (error) {
      });
    }


  }
};
</script>

<style lang="scss">
@import '../account';
</style>
